package com.yanqu.road.dao.impl.drugstore;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.DrugOrderNeed;
import com.yanqu.road.entity.drugstore.UserDrugChuFangOrderData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDrugChuFangOrderDataDaoImpl extends TempDataObjectDao<UserDrugChuFangOrderData> {

    public static String getUserDrugChuFangOrderDataSql = "select * from `t_u_drug_order` where `userId`=? and getReward = 0";
    public List<UserDrugChuFangOrderData> getUserDrugChuFangOrderData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserDrugChuFangOrderData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDrugChuFangOrderDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertUserDrugChuFangOrderDataSql = "insert into `t_u_drug_order`(userId,no,npc,tableId,chuFang,dialogId,chuFangLv,process,state,reward,getReward,orderTime,receiveTime,drugPlantStartTime,drugPlantDoneTime,drugMakeEndTime,getRewardTime,finishTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserDrugChuFangOrderData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNo()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNpc()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTable()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFang()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDialogId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFangLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProcess())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceiveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugPlantStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugPlantDoneTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugMakeEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFinishTime()));
        if (getDb().executeNoneQuery(insertUserDrugChuFangOrderDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDrugChuFangOrderDataSql = "update `t_u_drug_order` set npc=?,tableId=?,chuFang=?,dialogId=?,chuFangLv=?,process=?,state=?,reward=?,getReward=?,orderTime=?,receiveTime=?,drugPlantStartTime=?,drugPlantDoneTime=?,drugMakeEndTime=?,getRewardTime=?,finishTime=? where userId=? and no=?";

    @Override
    protected boolean updateData(UserDrugChuFangOrderData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getNpc()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTable()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFang()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDialogId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFangLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProcess())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceiveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugPlantStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugPlantDoneTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrugMakeEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNo()));
        if (getDb().executeNoneQuery(updateUserDrugChuFangOrderDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserDrugChuFangOrderData parseData(ResultSet rs) throws SQLException {
        UserDrugChuFangOrderData data = new UserDrugChuFangOrderData();
        data.setUserId(rs.getLong("userId"));
        data.setNo(rs.getLong("no"));
        data.setNpc(rs.getLong("npc"));
        data.setTable(rs.getInt("tableId"));
        data.setChuFang(rs.getInt("chuFang"));
        data.setDialogId(rs.getInt("dialogId"));
        data.setChuFangLv(rs.getInt("chuFangLv"));
        data.setProcess(JSON.parseObject(rs.getString("process"), new TypeReference<List<DrugOrderNeed>>() {}));
        data.setState(rs.getInt("state"));
        data.setReward(rs.getString("reward"));
        data.setGetReward(rs.getBoolean("getReward"));
        data.setOrderTime(rs.getLong("orderTime"));
        data.setReceiveTime(rs.getLong("receiveTime"));
        data.setDrugPlantStartTime(rs.getLong("drugPlantStartTime"));
        data.setDrugPlantDoneTime(rs.getLong("drugPlantDoneTime"));
        data.setDrugMakeEndTime(rs.getLong("drugMakeEndTime"));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        data.setFinishTime(rs.getLong("finishTime"));

        return data;
    }
}
